package com.blog.controller.active;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.blog.common.base.BaseController;
import com.blog.common.util.LayuiPageUtils;
import com.blog.entity.active.Active;
import com.blog.entity.active.ActiveType;
import com.blog.entity.vo.ActiveVo;
import com.blog.service.active.ActiveService;
import com.blog.service.active.ActiveTypeService;
import com.blog.service.imp.active.ActiveTagServiceImp;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/sunBlog")
public class IndexActiveController extends BaseController {

	@Autowired
	private ActiveService activeService;
	
	@Autowired
	private ActiveTypeService activeTypeService;

	@Autowired
	private ActiveTagServiceImp activeTagService;
	 
	//文章   跳转
	@RequestMapping(value = "", method = RequestMethod.GET)
 	public ModelAndView showIndx(HttpServletRequest request)
	{
		//默认加载第一个页的数据 
		Map<String,Object> parmsMap=new HashMap<>();
		parmsMap.put("limit","5");
		parmsMap.put("page","1");
		Map<String,Object> pageMap=LayuiPageUtils.PageMap(parmsMap);
		List<ActiveVo> list=activeService.selectIndexShowActive(pageMap);
		Map<String,Object> map=new HashMap<>();
		map.put("ActiveList", list);
		map.put("nextPage",1);
		String picUrl=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/hmp/pic/";  
		map.put("picUrl",picUrl);
		return view("showIndex/index",map);
	}  
	 
    @RequestMapping("/page/{num}")
	public ModelAndView selectIndexShowActive(@PathVariable int num)
	{
		Map<String,Object> parmsMap=new HashMap<>();
    	parmsMap.put("limit","5");
		parmsMap.put("page",num+1+"");
		Map<String,Object> map=new HashMap<>();
        //总数
		Integer count=activeService.countActive(map);
        //总页数
        int pageNum = count / 5;
        map.put("pageNum", pageNum);
        if (pageNum > num) {
        	map.put("nextPage", num + 1);
        }
		Map<String,Object> pageMap=LayuiPageUtils.PageMap(parmsMap);
		List<ActiveVo> list=activeService.selectIndexShowActive(pageMap);
		map.put("ActiveList", list);
		
		return view("showIndex/index",map);
	}

    /**
     * 
     * @author:彬
     * @time:2018年8月14日 下午3:12:40
     * 描述: 文章详细页面 
     */
    @RequestMapping("/active/{id}")
  	public ModelAndView activeShow(@PathVariable int id)
  	{
    	Map<String,Object> map=new HashMap<>();
    	Active active=activeService.selectShowMdText(id);
    	ActiveType  activeType=activeTypeService.selectActiveTypeById(active.getType());//这里直接用了已经 写好的 sql  就不重新写 
     
    	String[] stringTags=active.getTag().split("[,]");
    	active.setTypeName(activeType.getTypeName());
    	map.put("active", active);
    	map.put("tags", stringTags);
    	
  		return view("showIndex/article",map);
  	}

}
